package com.dingup.paike.handler;

import com.alibaba.fastjson.JSON;
import com.dingup.paike.model.feedback.SubjectQuestion;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by wancheng on 2018/6/23.
 */
public class SubjectQuestionsHandler extends BaseTypeHandler<List<SubjectQuestion>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<SubjectQuestion> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
    }

    @Override
    public List<SubjectQuestion> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return JSON.parseArray(rs.getString(columnName), SubjectQuestion.class);
    }

    @Override
    public List<SubjectQuestion> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return JSON.parseArray(rs.getString(columnIndex), SubjectQuestion.class);
    }

    @Override
    public List<SubjectQuestion> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return JSON.parseArray(cs.getString(columnIndex), SubjectQuestion.class);
    }
}
